﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using mscook.mvc.Core;

namespace mscook.mvc.Areas.weblogin.Controllers
{

    public class BulletinController : BaseController
    {
        //
        // GET: /weblogin/Bullet/
        //  protected IRepository<Bulletin> repository = UnityHelper.UnityToT<IRepository<Bulletin>>();

        private IBulletinRepository iBulletinRepository;
        private IImageSettingFactory iImageSettingFactory;
        private readonly string SectionName = "Bulletin";
        private ILoginUserService iLoginUserService;

        public BulletinController(IBulletinRepository iBulletinRepository, IImageSettingFactory iImageSettingFactory, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iBulletinRepository = iBulletinRepository;
            this.iImageSettingFactory = iImageSettingFactory;
            this.iLoginUserService = iLoginUserService;
        }
        public static Encoding encoder = Encoding.UTF8;

        public ActionResult Index(string nowPageNum, string per = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Bulletin> bulletinList = iBulletinRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);


            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            InitNavBar();
            return View(bulletinList);
        }

        //[AllowAnonymous] MembersShip权限判定
        //[AuthorizeAttribute(Roles = "admin")]

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult add()
        {
            InitNavBar();
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult add(Bulletin bulletin,bool IsTop)
        {
            if (ModelState.IsValid)
            {
                bulletin.EntityIndex = IsTop ? 1 : 0;
                SaveUploadImage(bulletin);
                iBulletinRepository.SaveOrUpdate(bulletin);

            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult edit(string id)
        {


            Bulletin bulletin = iBulletinRepository.Get(id);
            if (bulletin != null)
            {
                InitNavBar();
                return View(bulletin);
            }

            return RedirectToIndex();



        }
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult edit(FormCollection formValues, Bulletin modelValidate,bool IsTop)
        {

            if (ModelState.IsValid)
            {
                Bulletin bulletin = iBulletinRepository.Get(formValues["Id"]);
                SaveUploadImage(bulletin);

                UpdateModel(bulletin);
                bulletin.EntityIndex = IsTop ? 1 : 0;
                iBulletinRepository.SaveOrUpdate(bulletin);
          

            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iBulletinRepository.Delete(id);
            return RedirectToIndex();
        }

        public ActionResult BulletinModal(string nameInput, string idInput, string title, string linkBtn = "", string num = "")
        {

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "3");
            long total = 0;
            IList<Bulletin> cheflList = iBulletinRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total, title);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;

            return View(cheflList);
        }

        public bool SaveUploadImage(Bulletin bulletin)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file != null && file.InputStream != null && file.ContentLength != 0)
                {
                    bulletin.ImagePath = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                return true;

            }
            return false;
        }

        

    }
}
